home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / rrts00.zip / RRTS.BAS < prev    next >
BASIC Source File  |  1991-02-13  |  7KB  |  152 lines

  1. 'RRTS - Remove Redundant Trailing Spaces, vs. 0.0, by Jim Groeneveld, 12/2-91.
  2. 'E-mail: GROENEVELD@NIPG.TNO.NL (internet address)         _     _     _      _
  3. 'File xfer: GROENEVELD@HDETNO51.BITNET (EARN address)     | |   | |   | \    / |
  4. 'NIPG-TNO [postal/visiting address] | Y. Groeneveld       | |   | |   |  \  /  |
  5. 'P.O.Box 124    | Wassenaarseweg 56 | Schoolweg 14        | |   | |   |   \/   |
  6. '2300 AC Leiden | 2333 AL Leiden    | 8071 BC Nunspeet _  | |   | |   | |\  /| |
  7. 'Nederland (NL) | (+31|0)71-178810  | 03412-60413     | |_| |   | |   | | \/ | |
  8. '[office]     Fax (+31|0)71-176382  | [home]           \___/    |_|   |_|    |_|
  9.  
  10. ' Declare the Comline subprogram, as well as the number and
  11. ' type of its parameters.
  12. '''REM $DYNAMIC
  13. DECLARE SUB PrintEmbeddedSpaces (Length%, Nspaces&, OutLength&)
  14. DECLARE SUB Comline (N%, Args$(), Max%)
  15.  
  16. ' Default variable type is integer in this module.
  17. CLEAR : CLOSE : SCREEN 0: WIDTH 80: KEY OFF: DEFINT A-Z: OPTION BASE 1
  18.  
  19. CONST MaxLineInputLength = 32767 ''' 1000
  20. DimArgs = 15: DIM Args$(1 TO DimArgs)''', DataLine(1 TO 1) AS STRING * 255
  21.  
  22. PRINT "RRTS - Remove Redundant Trailing Spaces"
  23. PRINT "vs. 0.0, by Jim Groeneveld, 12/2-91"
  24. ' Get what was typed on the command line up to two parameters.
  25. CALL Comline(N, Args$(), 2)
  26.  
  27. ' Process arguments into file names
  28. IF Args$(1) = "" THEN
  29.   PRINT "Syntax: RRTS «input_file_name» «output_file_name»": BEEP: END
  30. ELSE
  31.   RRTSin$ = Args$(1)' not yet checked for legal syntax or file existence
  32. END IF
  33. IF Args$(2) = "" OR Args$(2) = RRTSin$ THEN
  34.   ' later: rename RRTSin to '.BAK' and so on; for the moment:
  35.   PRINT "Syntax: RRTS «input_file_name» «output_file_name»": BEEP: END
  36. ELSE
  37.   RRTSout$ = Args$(2)' not yet checked for legal syntax or file existence
  38. END IF
  39.  
  40. PRINT : PRINT "Processing "; RRTSin$; " into "; RRTSout$; "......"
  41. ' open input and output files
  42. OPEN "I", 1, RRTSin$: OPEN "O", 2, RRTSout$
  43.  
  44. ' read input and write output lines while processing contents
  45. LinesRead& = 0: LinesWritten& = 0: LineParts = 0: Nspaces& = 0: OutLength& = 0
  46. WHILE NOT EOF(1) ''': PRINT FRE(""), FRE(0), FRE(-1), FRE(-2)
  47.   ON ERROR GOTO out.of.string.space: LINE INPUT #1, DataLine$: ON ERROR GOTO 0: LenDataLine& = LEN(DataLine$)
  48. ''  DataLine$ = "": ON ERROR GOTO out.of.string.space: DataLine$ = INPUT$(MaxLineInputLength, #1): ON ERROR GOTO 0: LenDataLine& = LEN(DataLine$) ' other alternate
  49. '  DataLine$ = "": LenDataLine& = 0: FOR i = 1 TO MaxLineInputLength' alternate input of long string line (slow)
  50. '    DataChar$ = INPUT$(1, #1)
  51. '    IF DataChar$ = CHR$(13) THEN 'end_of_line
  52. '      DataChar$ = INPUT$(1, #1) ' reads chr$(10)
  53. '      EXIT FOR
  54. '    ELSE
  55. '      DataLine$ = DataLine$ + DataChar$
  56. '      LenDataLine& = LenDataLine& + 1
  57. '      IF LenDataLine& MOD 10 = 0 THEN LOCATE , 1: PRINT "Processing line"; LinesRead& + 1; "with length"; LineParts * MaxLineInputLength + LenDataLine&; ''': PRINT "into length"; OutLength&; SPACE$(10);
  58. '    END IF
  59. '  NEXT i
  60.  
  61.   IF LenDataLine& < MaxLineInputLength THEN
  62.     LinesRead& = LinesRead& + 1: LOCATE , 1: PRINT "Processing line"; LinesRead&; "with length"; LineParts * MaxLineInputLength + LenDataLine&;
  63.     WHILE RIGHT$(DataLine$, 1) = " ": DataLine$ = LEFT$(DataLine$, LEN(DataLine$) - 1): WEND' Remove Redundant Trailing Spaces
  64. '    FOR i = LEN(DataLine$) TO 1 STEP -1' check for spaces from the end
  65. '      IF MID$(DataLine$, i, 1) <> " " THEN LenDataLine& = i: EXIT FOR
  66. '    NEXT i
  67.      CALL PrintEmbeddedSpaces(LEN(DataLine$), Nspaces&, OutLength&)
  68. '    PRINT #2, LEFT$(DataLine$, LenDataLine&): LinesWritten& = LinesWritten& + 1
  69. '    PRINT LenDataLine&; SPACE$(10);
  70.     PRINT #2, DataLine$: LinesWritten& = LinesWritten& + 1: OutLength& = OutLength& + LEN(DataLine$)
  71.     PRINT "into length"; OutLength&; SPACE$(10);
  72.     LineParts = 0: Nspaces& = 0: OutLength& = 0
  73.   ELSE ' {LenDataLine&=MaxLineInputLength} partial line read only, cannot remove spaces because of still unknown contents of next part
  74.     LineParts = LineParts + 1
  75.     WHILE RIGHT$(DataLine$, 1) = " ": DataLine$ = LEFT$(DataLine$, LEN(DataLine$) - 1): WEND' Remove Redundant Trailing Spaces
  76.     CALL PrintEmbeddedSpaces(LEN(DataLine$), Nspaces&, OutLength&)
  77.     Nspaces& = Nspaces& + MaxLineInputLength - LEN(DataLine$)
  78.     PRINT #2, DataLine$; : OutLength& = OutLength& + LEN(DataLine$)
  79.   END IF
  80. WEND
  81.  
  82. PRINT : PRINT "End of program": CLOSE : END
  83.  
  84. out.of.string.space: PRINT #2, "*** RRTS - part of long line (>±23000 bytes) lost *** "; : RESUME' NEXT
  85.  
  86. SUB Comline (NumArgs, Args$(), MaxArgs) 'STATIC
  87. CONST TRUE = -1, FALSE = 0
  88. NumArgs = 0: In = FALSE: Quote$ = ""
  89. ' Get the command line using the COMMAND$ function.
  90. Cl$ = COMMAND$: L = LEN(Cl$)
  91. ' Go through the command line a character at a time.
  92. FOR i = 1 TO L
  93.   C$ = MID$(Cl$, i, 1)
  94.   IF Quote$ <> "" THEN ' Quote is ' or ", wich means: within argument string
  95.     IF C$ = Quote$ THEN
  96.       ' Check for doubled embedded quotes (if I<L)
  97.       IF i < L THEN
  98.         IF MID$(Cl$, i + 1, 1) = Quote$ THEN ' is quote part of doubled one?
  99.           ' add one of the doubled quotes to the argument
  100.           Args$(NumArgs) = Args$(NumArgs) + C$
  101.           i = i + 1' increase character counter
  102.         ELSE ' no doubled quote
  103.           ' end of quoted argument (not yet doubling of embedded quotes)
  104.           Quote$ = ""' Single or double quote not returned as part of the Args$
  105.         END IF
  106.       ELSE ' end of command line reached
  107.         ' end of quoted argument (not yet doubling of embedded quotes)
  108.         Quote$ = ""' Single or double quote not returned as part of the Args$
  109.       END IF
  110.     ELSE
  111.       ' part of quoted argument
  112.       Args$(NumArgs) = Args$(NumArgs) + C$
  113.     END IF
  114.   ELSE ' Quote=""
  115.     ' Test for character being a blank or a tab or a comma.
  116.     IF (C$ <> " " AND C$ <> CHR$(9) AND C$ <> ",") THEN
  117.       ' Neither blank nor tab or comma.
  118.       ' Test to see if you're already inside an argument.
  119.       IF NOT In THEN
  120.         ' You've found the start of a new argument.
  121.         ' Test for too many arguments.
  122.         IF NumArgs = MaxArgs THEN EXIT FOR
  123.         NumArgs = NumArgs + 1
  124.         In = TRUE
  125.       END IF
  126.       ' Test for character being a single or double quote.
  127.       IF (C$ = "'" OR C$ = CHR$(34)) THEN
  128.         Quote$ = C$' Single or double quote not returned as part of the Args$
  129.       ELSE
  130.         ' Add the character to the current argument.
  131.         Args$(NumArgs) = Args$(NumArgs) + C$
  132.       END IF
  133.     ELSE
  134.       ' Found a blank or a tab or a comma, not part of an argument.
  135.       ' Set "Not in an argument" flag to FALSE.
  136.       In = FALSE
  137.     END IF
  138.   END IF
  139. NEXT i
  140. END SUB
  141.  
  142. SUB PrintEmbeddedSpaces (Length, Nspaces&, OutLength&)
  143. IF Length > 0 THEN ' firstly print embedded spaces
  144.   OutLength& = OutLength& + Nspaces&
  145.   FOR i& = 1 TO INT(Nspaces& / MaxLineInputLength)
  146.     PRINT #2, SPACE$(MaxLineInputLength);
  147.   NEXT i&: PRINT #2, SPACE$(Nspaces& MOD MaxLineInputLength);
  148.   Nspaces& = 0
  149. END IF
  150. END SUB
  151.  
  152.